﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using DAL;
using Common;
namespace UI
{
    public partial class open2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["code"] == null)
            {
                Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1a4dea3fd54864b9&redirect_uri=" + Server.UrlEncode("http://www.kdxny84.cn/open2.aspx?youropenid=" + Request["youropenid"] + "&authorizationappid="+ Request["authorizationappid"]) + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
            }
            else
            {
                WeiXin wx = new WeiXin();
                string result = wx.HttpGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx1a4dea3fd54864b9&secret=75fac3337cd918fd762b48f368cba18e&code=" + Request["code"] + "&grant_type=authorization_code", "");
                string ouropenid = wx.ConvertJson(result).openid;
                string youropenid = Request["youropenid"];
                string authorizationappid = Request["authorizationappid"];
                if (!OpenidDAL.IsExist(youropenid, ouropenid))
                {
                    OpenidInfo info = new OpenidInfo()
                    {
                        ID = 0,
                        YourOpenID = youropenid,
                        OurOpenID = ouropenid,
                        AuthorizationAppID = authorizationappid,
                        CreateDate = DateTime.Now
                    };
                    int ID = OpenidDAL.Save(info);
                }
                
            }
        }
    }
}